﻿/************************************************************************
 * 项目名称 :  Unity  
 * 项目描述 :     
 * 类 名 称 :  NotificationExtensions
 * 版 本 号 :  v1.0.0.0 
 * 说    明 :     
 * 作    者 :  戴璐宇
 * 创建时间 :  2021/6/7 23:29:03
 * 更新时间 :  2021/6/7 23:29:03
************************************************************************
 * Copyright @ Aatsr 2021. All rights reserved.
************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace Unity
{
    public static class NotificationExtensions
    {
        public static void Notify(this PropertyChangedEventHandler eventHandler, Expression<Func<object>> expression)
        {
            if (null == eventHandler)
            {
                return;
            }
            var lambda = expression as LambdaExpression;
            MemberExpression memberExpression;
            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = lambda.Body as UnaryExpression;
                memberExpression = unaryExpression.Operand as MemberExpression;
                memberExpression = unaryExpression.Operand as MemberExpression;
            }
            else
            {
                memberExpression = lambda.Body as MemberExpression;
            }
            var constantExpression = memberExpression.Expression as ConstantExpression;
            var propertyInfo = memberExpression.Member as PropertyInfo;

            foreach (var del in eventHandler.GetInvocationList())
            {
                del.DynamicInvoke(new object[] { constantExpression.Value, new PropertyChangedEventArgs(propertyInfo.Name) });
            }
        }

    }
}

